<template>
    <common-card title="累计用户数" :value="numberFormat(userTotal)">
        <div ref="barChartRef" style="width:100%;height:100%"></div>
        <template #footer>
            <div class="footer">
                <span>日同比</span>
                <span class="emphasis">{{ userGrowthLastDay }}%</span>
                <span class="increment"></span>
                <span style="margin-left:10px">月同比</span>
                <span class="emphasis">{{ userGrowthLastMonth }}%</span>
                <span class="decrement"></span>
            </div>
        </template>
    </common-card>
</template>

<script lang="ts">
import CommonCard from './CommonCard.vue'
export default {
    name: 'TotalUsers',
    components: {
        CommonCard
    }
}
</script>

<style lang="scss">
.footer {
    display: flex;
    // flex:1;
    align-items: center;
}
</style>

<script lang="ts" setup>
import * as echarts from 'echarts'
import type { EChartsType } from 'echarts'
import { ref, onMounted, computed, watch } from 'vue'
import { useReportDataStore } from '@/stores/reportData'
import useFormat from '@/hooks/useFormat'
const reportDataStore = useReportDataStore()
const { numberFormat, moneyFormat } = useFormat()


// 累计用户数
const userTotal = computed(() => reportDataStore.reportData.usersTotal)
const usersLastMonth = computed(() => reportDataStore.reportData.usersLastMonth)
const userGrowthLastDay = computed(() => reportDataStore.reportData.userGrowthLastDay)
const userGrowthLastMonth = computed(() => reportDataStore.reportData.userGrowthLastMonth)
// 定义变量
const barChartRef = ref()
let myChart: EChartsType
const getOption = () => {
    return {
        grid: {
            left: 0,
            right: 0
        },
        xAxis: {
            min: 0,
            max: 100,
            show: false
        },
        yAxis: {
            show: false,
            // 将X轴的数据项给到Y轴，从而让柱条以水平方向和呈现
            data: ['数据']
        },
        series: {
            type: 'bar',
            data: [38],
            // 设置柱条的宽度
            barWidth: 10,
            // 显示柱条的背景颜色
            showBackground: true,
            itemStyle: {
                color: 'skyblue'
            },
            // 设置刻度样式
            label: {
                show: true,
                // 把文字改成竖线
                formatter: '|',
                color: 'orangered',
                position: 'right'
            },

        },
        // 显示提示
        tooltip: {
            trigger: 'axis',
            confine: true
        }
    }
}

// 组件挂载完毕就要显示图形
onMounted(() => {
    myChart = echarts.init(barChartRef.value)
    myChart.setOption(getOption())

    // 当浏览器宽度发生变化的时候，图表也要发生变化
    window.addEventListener('resize', () => {
        myChart && myChart.resize()
    })
})
</script>

<style lang="scss" scoped>
.footer {
    display: flex;
    align-items: center;
}
</style>